<?php
require_once 'ApibaseController.php';

/**
 * 情報誌問い合わせAPI<br />
 *
 * 情報誌問い合わせAPI
 *
 * @package apamanshop
 * @author ushijima
 * @copyright コピーライト(要確認)
 */
class Inquiry_ApimagazineController extends Inquiry_ApibaseController
{
	/**
	 * 情報誌問合せAPIアクション
	 *
	 * @access public
	 * @return void
	 */
	public function indexAction()
	{

		// 変数初期化処理 start

		$strPlaceKbn = $this->_params['PlaceKbn'];								//地域ｻｲﾄID
		$sSoufuJyouhoushi = $this->_params['SoufuKibouJyouhoushi'];				//送付希望情報誌
		$sSoufuJyouhoushiText = $this->_params['SoufuKibouJyouhoushiText'];		//送付希望情報誌ﾃｷｽﾄ
		$sNameSei = $this->_params['NameSei'];									//名称(姓)
		$sNameMei = $this->_params['NameMei'];									//名称(名)
		$sKanaSei = $this->_params['KanaSei'];									//ｶﾅ名称(姓)
		$sKanaMei = $this->_params['KanaMei'];									//ｶﾅ名称(名)
		$sAge = $this->_params['AgeKbn'];										//年齢
		$sAgeText = $this->_params['AgeNm'];									//年齢ﾃｷｽﾄ
		$sSeibetsu = $this->_params['Sex'];										//性別
		$sSeibetsuText = $this->_params['SexNm'];								//性別ﾃｷｽﾄ
		$sKekkon = $this->_params['Mrrg'];										//結婚
		$sKekkonText = $this->_params['MrrgNm'];								//結婚ﾃｷｽﾄ
		$sJob = $this->_params['JobKbn'];										//職業
		$sJobText = $this->_params['JobNm'];									//職業ﾃｷｽﾄ
		$sYuubin1 = $this->_params['Zip1'];										//郵便番号1
		$sYuubin2 = $this->_params['Zip2'];										//郵便番号2
		$sTodoufuken = $this->_params['PrefCd'];								//都道府県
		$sTodoufukenText = $this->_params['PrefName'];							//都道府県ﾃｷｽﾄ
		$sJyuushoText = $this->_params['Address'];								//住所ﾃｷｽﾄ
		$aTel = $this->_params['Tel'];											//TEL
		$sFax = $this->_params['Fax']; 											//FAX
		$sMail = $this->_params['Mail'];										//Mail
		$sComment = $this->_params['Comment'];									//ご意見・ご感想
		$sHikkoshiRiyuu = $this->_params['HikkoshiRiyuu'];						//引越理由
		$sHikkoshiRiyuuSonota = $this->_params['HikkoshiRiyuuSonota'];			//引越理由(その他)
		$sHikkoshiRiyuuText = $this->_params['HikkoshiRiyuuText'];				//引越理由ﾃｷｽﾄ
		$sKounyuuKeiken = $this->_params['KounyuuKeiken'];						//購入経験
		$sKounyuuKeikenText = $this->_params['KounyuuKeikenText'];				//購入経験ﾃｷｽﾄ
		$sKikkake = $this->_params['Kikkake'];									//知ったきっかけ
		$sKikkakeSonota = $this->_params['KikkakeSonota'];						//知ったきっかけ(その他)
		$sKikkakeText = $this->_params['KikkakeText'];							//知ったきっかけﾃｷｽﾄ
		$sSonotaZasshi = $this->_params['SonotaZassi'];							//参考雑誌
		$sSonotaZasshiName = $this->_params['SonotaZassiName'];					//参考雑誌名称
		$sSonotaZasshiTex = $this->_params['SonotaZassiText'];					//参考雑誌ﾃｷｽﾄ
		$sJyuushiKoumoku = $this->_params['JyuushiKoumoku'];					//重視項目
		$sJyuushioumokuSonota = $this->_params['JyuushiKoumokuSonota'];			//部屋探し重視項目(そのｼ)
		$sJyuushiKoumokuText = $this->_params['JyuushiKoumokuText'];			//部屋探し重視項目ﾃｷｽﾄ
		$sNeedTokusyuu = $this->_params['NeedTokushuu'];						//掲載して欲しい物件の特集記事
		$sNeedTokusyuuSonota = $this->_params['NeedTokushuuSonota'];			//掲載して欲しい物件の特W記事(その他)
		$sNeedTokusyuuText = $this->_params['NeedTokushuuText'];				//掲載して欲しい物件の特集L事ﾃｷｽﾄ

		//送信時刻
		$strTimeStr = $this->_params['TimeStr'];

		//--- メール判定 ---
		$this->setSendMailFlag($this->_params['Mail']);

		$add_params = array();

		$add_params['Name'] = $sNameSei . "　" . $sNameMei;
		$add_params['NameKn'] = $sKanaSei . "　" . $sKanaMei;
		$add_params['Zip'] = $sYuubin1 . "-" . $sYuubin2;

		// ﾛｸﾞ情報格納処理 *****************************************
		try {
			// モデルの呼び出し
			$this->_model = new Inquiry_Model_Apiinquiry();

			//トランザクション開始
			$db = $this->_model->getDbConnection();
			$this->_model->startTransaction($db);

			if ($this->isLogIns()) {
				//対応状況TBLﾃﾞｰﾀ挿入処理
				$resApi = $this->_model->insJo($db,$this->_params,$add_params);
			}

			// ﾒｰﾙ送信処理 ********************************************
			$result =  $this->SendMailMain($sMail,$add_params);
			if($result == "0"){
				 $this->raiseError('メール送信エラー');
			}

			__log_debug(__FILE__, __LINE__, __METHOD__, "コミットします");
			$this->_model->commitTransaction($db);
			$db->closeConnection();

			$result = 1;

		} catch (Exception $e) {
			__log_debug(__FILE__, __LINE__, __METHOD__, "ロールバックします");
			$this->_model->rollbackTransaction($db);
			$db->closeConnection();
			$result = 0;
		}

		// 返却データ作成
		$resResult = array('api_result' => array('result' => $result,'res_detail' => $resApi));
		// 返却処理
		$this->_api_data = $resResult;
		$this->display();

	}

	//メール送信制御
	private function SendMailMain($mail,$addinfo) {
		// ｽﾃｰﾀｽ初期化

		// ユーザへメール送信
		if ($mail != "") {
			$userResult = $this->SendMailUser($mail,$addinfo);
		}

		// 本部へのメール送信
		$officeResult = $this->SendMailOffice($addinfo);

		$result = 1;
		if ( $userResult == 0 || $officeResult == 0) {
			$result = 0;
		}
		return $result;
	}

	// 問い合わせユーザへのメール送信
	private function SendMailUser($mail,$addinfo) {

		// from(送信元)、to（あて先)
		$strMailTo = array($mail => "");								//送信先ﾒｰﾙｱﾄﾞﾚｽ
		$strMailFrom = array(Apm_Config_Mail::MAIL_JYOUHOUSHI => "");	//送信元ﾒｰﾙｱﾄﾞﾚｽ


		// メール本文に必要なパラメータ
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_SUBJECT_KEY] =  "情報誌「アパマンショップ」お申し込みのご確認";
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_TO_KEY] = $strMailTo;
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_FROM_KEY] = $strMailFrom;

		// ﾒｰﾙ送信処理
		if($this->isSendUser() == true) {
			$result = $this->sendToiawaseMail("MailMagazineUser.tpl",array_merge($this->_params,$addinfo), $mail_params);
		}

		return $result;
	}

	// 本部のメール送信
	private function SendMailOffice($addinfo) {

		$strSubject = "【情報誌送付申し込み】";
		$strMailTo =   array(Apm_Config_Mail::MAIL_JYOUHOUSHI => "");	//送信先ﾒｰﾙｱﾄﾞﾚｽ
		$strMailFrom = array(Apm_Config_Mail::MAIL_JYOUHOUSHI => "");	//送信元ﾒｰﾙｱﾄﾞﾚｽ

		//@ 送信先ﾒｰﾙｱﾄﾞﾚｽ
		// 本文の設定
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_SUBJECT_KEY] = $strSubject;
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_TO_KEY] = $strMailTo;
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_FROM_KEY] = $strMailFrom;

		if($this->isSendHonbu() == true) {
			// ﾒｰﾙ送信処理
			$result = $this->sendToiawaseMail("MailMagazineShop.tpl",array_merge($this->_params,$addinfo), $mail_params);
		}else if($this->getReplaceHonbuMail() != "") {
			$mail_params[Apm_Otheritems::SEND_MAIL_EX_TO_KEY] = array($this->getReplaceHonbuMail() => "");
			// ﾒｰﾙ送信処理
			$result = $this->sendToiawaseMail("MailMagazineShop.tpl",array_merge($this->_params,$addinfo), $mail_params);
		}

		return $result;
	}
}
?>
